package gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import app.Cookbook;
import app.Recipe;
import net.miginfocom.swing.MigLayout;

/********************************************************
 * 
 * Project : TeamProject Package : gui File : SearchPanel.java Name : Tyler
 * Daines Date : Nov 5, 2013
 * 
 * 
 ********************************************************/
public class SearchPanel extends JPanel implements ActionListener, MouseListener, FocusListener, KeyListener
{
	private JTextField searchNameField, searchIngredientField;
	private JButton searchName, searchIngredient, searchCategory;
	private JList<Object> searchResults;
	private JComboBox<String> categoryBox;
	private Cookbook cookbook;

	/********************************************************
	 * Name : SearchPanel Purpose : Constructor Parameters :
	 ********************************************************/
	public SearchPanel()
	{
		setLayout(new MigLayout());
		cookbook = new Cookbook();
		initTextField();
		initButtons();
		initList();
		initCombo();
		addComponents();
	}

	/********************************************************
	 * Name : initTextField Purpose : Parameters : Returns : void
	 ********************************************************/
	private void initTextField()
	{
		searchNameField = new JTextField();
		searchNameField.setMinimumSize(new Dimension(200, 25));
		searchNameField.addFocusListener(this);
		searchNameField.addKeyListener(this);
		
		searchIngredientField = new JTextField();
		searchIngredientField.setMinimumSize(new Dimension(200, 25));
		searchIngredientField.addFocusListener(this);
		searchIngredientField.addKeyListener(this);

	}

	/********************************************************
	 * Name : initButtons Purpose : Parameters : Returns : void
	 ********************************************************/
	private void initButtons()
	{

		searchName = new JButton("Search by Name");
		searchName.addActionListener(this);

		searchIngredient = new JButton("Search by Ingredient");
		searchIngredient.addActionListener(this);

		searchCategory = new JButton("Search by Category");
		searchCategory.addActionListener(this);
		
		searchName.setEnabled(false);
		searchIngredient.setEnabled(false);

	}

	/********************************************************
	 * Name : initList Purpose : Parameters : Returns : void
	 ********************************************************/
	private void initList()
	{
		searchResults = new JList<>();
		searchResults.setMinimumSize(new Dimension(400, 500));
		searchResults.addMouseListener(this);
		searchResults.addKeyListener(this);
		
		ArrayList<Recipe> allRecipes = cookbook.viewAllRecipes();
		allRecipes = (ArrayList<Recipe>) sortList(allRecipes);
		searchResults.setListData(allRecipes.toArray());
		

	}

	/********************************************************
	 * Name : initCombo Purpose : Parameters : Returns : void
	 ********************************************************/
	private void initCombo()
	{
		categoryBox = new JComboBox<String>();
		categoryBox.setMinimumSize(new Dimension(200, 25));
//		categoryBox.addItem("Select a Category");
//		cookbook.pullCategories(categoryBox);
		
		HashMap<Integer, String> categoriesMap = cookbook.getCategoryHashMap();
		String[] comboBoxArray = new String[categoriesMap.size()];
		comboBoxArray = categoriesMap.values().toArray(comboBoxArray);
		categoryBox = new JComboBox<>(comboBoxArray);

	}

	/********************************************************
	 * Name : addComponents Purpose : Parameters : Returns : void
	 ********************************************************/
	private void addComponents()
	{
		add(searchNameField, "cell 0 0");
		add(searchIngredientField, "cell 1 0");
		
		add(searchName, "cell 0 1");
		add(searchIngredient, "cell 1 1");

		add(categoryBox, "cell 0 2");

		add(searchCategory, "cell 0 3");


		add(searchResults, "cell 1 3 4 10");

	}

	/********************************************************
	* Name       : sortList
	* Purpose    : 
	* Parameters : 
	* Returns    : List<Recipe>
	********************************************************/
	private List<Recipe> sortList(List<Recipe> recipes)
	{
		Collections.sort(recipes);
		
		return recipes;
	}

	@Override
	public String getName()
	{
		return "Search";
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseClicked(MouseEvent e)
	{
		if(e.getClickCount() == 2)
		{
			if(searchResults.getSelectedValue() instanceof Recipe)
			{
				Gui.createTab(new RecipePanel((Recipe) searchResults.getSelectedValue()));
			}
			
		}
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseEntered(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseExited(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
	@Override
	public void mousePressed(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseReleased(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == searchCategory)
		{
			searchForCategory();
		}
		
		if ( e.getSource() == searchIngredient)
		{
			searchForIngredient();
			
		}
		
		if(e.getSource() == searchName)
		{
			searchForName();
		}
		
		
		
	}

	/**
	* Name       : searchForCategory
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	**/
	private void searchForCategory()
	{
		int categorySelection = categoryBox.getSelectedIndex();
		ArrayList<Recipe> matches = cookbook.searchByCategory(categorySelection);
		matches = (ArrayList<Recipe>) sortList(matches);
		searchResults.setListData(matches.toArray());
		if(searchResults.getModel().getSize() == 0)
		{
			JOptionPane.showMessageDialog(this.getParent(), "Recipe(s) not found.");
		}
		validate();
	}

	/**
	* Name       : searchForName
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	**/
	private void searchForName()
	{
		String name = searchNameField.getText();
		ArrayList<Recipe> matches = cookbook.searchByName(name);
		matches = (ArrayList<Recipe>) sortList(matches);
		searchResults.setListData(matches.toArray());
		if(searchResults.getModel().getSize() == 0)
		{
			JOptionPane.showMessageDialog(this.getParent(), "Recipe(s) not found.");
		}
		validate();
	}

	/**
	* Name       : searchForIngredient
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	**/
	private void searchForIngredient()
	{
		if(!searchIngredientField.getText().equals(""))
		{
			int categorySelection = categoryBox.getSelectedIndex();
			String ingredient = searchIngredientField.getText();
			try
			{
				List<Recipe> matches = cookbook.searchRecipeByIngredient(ingredient, categorySelection);
				matches = (ArrayList<Recipe>) sortList(matches);
				searchResults.setListData(matches.toArray());
				if(searchResults.getModel().getSize() == 0)
				{
					JOptionPane.showMessageDialog(this.getParent(), "Recipe(s) not found.");
				}
				validate();
			}
			catch (SQLException e1)
			{
				e1.printStackTrace();
			}
		}
		else
		{
			JOptionPane.showMessageDialog(this.getParent(), "Please enter something in the field.");
		}
	}

	/* (non-Javadoc)
	 * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
	 */
	@Override
	public void focusGained(FocusEvent e)
	{
		if(e.getSource() == searchNameField)
		{
			searchName.setEnabled(true);
		}
		if(e.getSource() == searchIngredientField)
		{
			searchIngredient.setEnabled(true);
		}
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
	 */
	@Override
	public void focusLost(FocusEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/**
	* Name :keyPressed
	*@see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
	**/
	@Override
	public void keyPressed(KeyEvent e)
	{
		if(e.getKeyCode() == KeyEvent.VK_ENTER)
		{
			if(e.getSource() == searchResults)
			{
				if(searchResults.getSelectedValue() instanceof Recipe)
				{
					Gui.createTab(new RecipePanel((Recipe) searchResults.getSelectedValue()));
				}
			}
			
			if(e.getSource() == searchNameField)
			{
				searchForName();
			}
			
			if(e.getSource() == searchIngredientField)
			{
				searchForIngredient();
			}
		}
		
	}

	/**
	* Name :keyReleased
	*@see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
	**/
	@Override
	public void keyReleased(KeyEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/**
	* Name :keyTyped
	*@see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
	**/
	@Override
	public void keyTyped(KeyEvent e)
	{
		// TODO Auto-generated method stub
		
	}
	
}
